package qingke.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import qingke.bean.Page;
import qingke.mybatis.pojo.Product;
import qingke.util.Const;
import qingke.util.GetEncoder;
import service.ProductService;

@Controller
@RequestMapping("/Product")
public class ProductController {

	@Autowired
	private ProductService productService;

	@RequestMapping("/getAllProduct")
	public ModelAndView getAllStore(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/product.jsp");
		Page productPage = new Page(this.productService.countProducts(), Const.PAGE_PRODUCT_AMOUNT);
		List<Product> products = this.productService.productPaging(productPage);
		request.getSession().setAttribute("productPage", productPage);
		request.getSession().setAttribute("products", products);
		return mv;
	}

	@RequestMapping("/getProductAjax")
	public ModelAndView getStoreAjax(@RequestParam int NumberOfPage, HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		ModelAndView mv = new ModelAndView();
		String selectname = (String) request.getSession().getAttribute("select");
		Page OldproductPage = (Page) request.getSession().getAttribute("productPage");
		Page productPage;
		if (selectname == null) {
			productPage = new Page(this.productService.countProducts(), Const.PAGE_PRODUCT_AMOUNT);
		} else {
			productPage = new Page(this.productService.countProductsBySelect(selectname + "%"),
					Const.PAGE_PRODUCT_AMOUNT);
		}
		if (OldproductPage.getTotalAmount() != productPage.getTotalAmount()) {
			productPage.setCurrentPage(OldproductPage.getCurrentPage());
			productPage.setCurrentPosition(OldproductPage.getCurrentPosition());
		} else {
			productPage = OldproductPage;
		}

		Integer numberOfPage = productPage.getCurrentPage();
		if (NumberOfPage != 0) {
			numberOfPage = NumberOfPage;
		}
		if (numberOfPage > productPage.getTotalPage()) {
			numberOfPage = 1;
		}
		productPage.setCurrentPage(numberOfPage);
		productPage.setCurrentPosition(productPage.getCurrentPosition());
		if (selectname != null) {
			mv.setViewName("redirect:/product.jsp?select=" + GetEncoder.getUtf8(selectname));
			Map<String, Object> map = new HashMap<>();
			map.put("selectname", selectname + "%");
			map.put("page", productPage);
			List<Product> products = this.productService.productPagingBySelect(map);
			request.getSession().setAttribute("productPage", productPage);
			request.getSession().setAttribute("products", products);
		} else {
			mv.setViewName("redirect:/product.jsp");
			List<Product> products = this.productService.productPaging(productPage);
			request.getSession().setAttribute("productPage", productPage);
			request.getSession().setAttribute("products", products);
		}
		return mv;

	}

	@RequestMapping("/getAllProductBySelect")
	public ModelAndView getAllProductBySelect(@RequestParam String selectname, HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/product.jsp?select=" + GetEncoder.getUtf8(selectname));
		selectname = selectname + "%";

		Page productPage = new Page(this.productService.countProductsBySelect(selectname), Const.PAGE_STORE_AMOUNT);
		Map<String, Object> map = new HashMap<>();
		map.put("selectname", selectname);
		map.put("page", productPage);
		List<Product> products = this.productService.productPagingBySelect(map);
		request.getSession().setAttribute("productPage", productPage);
		request.getSession().setAttribute("products", products);
		return mv;
	}

}
